clear all

*Set working folders. Please change the global variable "user" to the folder with the replication package
global user "C:\Users\weilingliu1990\Dropbox\Insurance Project\LTCI Replication Package"
global data "$user\Data"
global images "$user\Images"
global drafttab "$user\Tables"

set maxvar  32767
*ssc install estout						//will need to install if you don't already have it


//TABLE 1C
use "$data\political_data", clear
estpost tabstat  yrs_left_in_term  democrat winpct margin contributions cashonhand , columns(statistics) statistics(mean sd min p25 p50 p75 max n) 
esttab using "$drafttab\Table_1C.tex", cells("mean(fmt(%9.2fc)) sd(fmt(%9.2fc)) min(fmt(%9.2fc)) p50(fmt(2)) p25(fmt(%9.2fc)) p75(fmt(%9.2fc)) max(fmt(%9.2fc)) count(fmt(%9.0fc))") noobs nomtitle nonumber label replace
eststo clear

//TABLE 1D
use "$data\SERF_ad_dropouts", clear
gen dropped=dropped_year<10000
replace dropped_year=. if dropped==0
estpost tabstat dropped dropped_year, columns(statistics) statistics(mean sd min p25 p50 p75 max n) 
esttab using "$drafttab\Table_1D.tex", cells("mean(fmt(%9.2fc)) sd(fmt(%9.2fc)) min(fmt(%9.2fc)) p50(fmt(2)) p25(fmt(%9.2fc)) p75(fmt(%9.2fc)) max(fmt(%9.2fc)) count(fmt(%9.0fc))") noobs nomtitle nonumber label replace
eststo clear

//FIGURE 6
use "$data\dropout_graph_data", clear
binscatter dropped2 ratio, control(i.state_num) xtitle("Avg Approved Increase/Requested Increase (%)") ytitle("Probability of Dropout (%)")
graph export "$images\Figure_6A.png", replace
binscatter dropped2 pct_approved, control(i.state_num) xtitle("Pct of Requests Approved (\%)") ytitle("Probability of Dropout (%)")
graph export "$images\Figure_6B.png", replace


//TABLE 11
use "$data\dropout_data2", clear
replace dropped2=dropped2/100
eststo: reg dropped2 appr_rate applied hist_rate i.state_num i.year i.co_code, vce(cluster co_code)
quietly summ dropped2
estadd scalar ysum = r(mean)
estadd local fe "Yes"
estadd local cofe "Yes"
estadd local stfe "Yes"
eststo: reg dropped2 ratio applied hist_rate i.state_num i.year i.co_code, vce(cluster co_code)
quietly summ dropped2
estadd scalar ysum = r(mean)
estadd local fe "Yes"
estadd local cofe "Yes"
estadd local stfe "Yes"
eststo: reg dropped2 pct_approved applied hist_rate i.state_num i.year i.co_code, vce(cluster co_code)
quietly summ dropped2
estadd scalar ysum = r(mean)
estadd local fe "Yes"
estadd local cofe "Yes"
estadd local stfe "Yes"
egen double_cluster3=group(state year)
eststo: reghdfe dropped2 appr_rate applied hist_rate i.co_code, absorb(double_cluster3)  vce(cluster double_cluster3)
quietly summ dropped2
estadd scalar ysum = r(mean)
estadd local fe "Yes"
estadd local cofe "Yes"
estadd local stfe "Yes"
eststo: reghdfe dropped2 ratio applied hist_rate i.co_code, absorb(double_cluster3) vce(cluster double_cluster3)
quietly summ dropped2
estadd scalar ysum = r(mean)
estadd local fe "Yes"
estadd local cofe "Yes"
estadd local stfe "Yes"
eststo: reghdfe dropped2 pct_approved applied hist_rate  i.co_code, absorb(double_cluster3) vce(cluster double_cluster3)
quietly summ dropped2
estadd scalar ysum = r(mean)
estadd local fe "Yes"
estadd local cofe "Yes"
estadd local stfe "Yes"

esttab using "$drafttab\Table_11.tex" , b(%9.4f) se(%9.4f) star(* 0.10 ** 0.05 *** 0.01) ///
scalars("ysum Mean Dependent Variable" "fe Year FE" "cofe Company FE" "stfe State FE" "N Number of Observations") ///
sfmt(%4.2f %11.2gc %11.2gc %11.0gc %11.2gc %11.3fc) noobs lines ///
label keep(ratio appr_rate pct_approved applied) nogaps numbers nobaselevels f ///
mgroups("Dummy for Dropped Next Year", pattern(1 0) span prefix(\multicolumn{@span}{c}{) suffix(}) erepeat(\cline{@span}) lhs( \\) ) ///
nomtitles replace
eststo clear


//TABLE 12
use "$data\dropout_data", clear
eststo: reg dropped2 yrs_left i.state_num i.year i.co_code, vce(cluster co_code)
quietly summ dropped2
estadd scalar ysum = r(mean)
estadd local fe "Yes"
estadd local cofe "Yes"
estadd local stfe "Yes"
eststo: reg dropped2 democrat i.state_num i.year i.co_code, vce(cluster co_code) 
quietly summ dropped2
estadd scalar ysum = r(mean)
estadd local fe "Yes"
estadd local cofe "Yes"
estadd local stfe "Yes"
eststo: reg dropped2 chg_dem chg_rep democrat i.state_num i.year i.co_code, vce(cluster co_code) 
quietly summ dropped2
estadd scalar ysum = r(mean)
estadd local fe "Yes"
estadd local cofe "Yes"
estadd local stfe "Yes"
eststo: reg dropped2 yrs_left democrat chg_dem chg_rep i.state_num i.year i.co_code, vce(cluster co_code) 
margins, dydx(*)
quietly summ dropped2
estadd scalar ysum = r(mean)
estadd local fe "Yes"
estadd local cofe "Yes"
estadd local stfe "Yes"

esttab using "$drafttab\Table_12.tex" , b(%9.4f) se(%9.4f) star(* 0.10 ** 0.05 *** 0.01) ///
scalars("ysum Mean Dependent Variable" "fe Year FE" "cofe Company FE" "stfe State FE" "N Number of Observations") ///
sfmt(%4.2f %11.2gc %11.2gc %11.0gc %11.2gc %11.3fc) noobs lines ///
label keep(chg_* democrat yrs_left_in_term) nogaps numbers nobaselevels f ///
mgroups("Dummy for Dropped Next Year", pattern(1 0) span prefix(\multicolumn{@span}{c}{) suffix(}) erepeat(\cline{@span}) lhs( \\) ) ///
nomtitles replace
eststo clear
